<template>
  <div class="line_wrap">
    <h1>课程信息统计</h1>
    <div id="myline">

    </div>
  </div>
</template>

<script>
import { getCourseList } from '../../api/course'
export default {
  name: 'JsxyLine',

  data () {
    return {

    };
  },

  mounted () {
    this.makeLine();
  },

  methods: {
    async makeLine () {
      var res = await getCourseList({ pageStart: 1 });
      var count = res.data.data.count;//总数据条数据
      var pageSize = res.data.data.pageSize;//一页的条数据
      var x = Math.ceil(count / pageSize) //总页码  8
      var arr = res.data.data.list;//所有数据
      for (var i = 2; i <= x; i++) {
        var myres = await getCourseList({ pageStart: i });
        console.log(myres)
        arr = [...arr, ...myres.data.data.list];
      }
      console.log(arr);
      var myarr = arr.filter((item) => {
        return item.CategoryOne
      })
      // console.log(myarr)
      // 处理日期 
      var datearr = myarr.map((item) => {
        return item.createAt.substr(0, 10);
      })
      var dset = new Set(datearr) //["2021-12-06","2021-12-07"]
      // console.log(dset)
      var darr = Array.from(dset).reverse();
      // 判断日期下的  课程数量
      var qianarr = [];
      var houarr = [];
      var shiarr = []
      darr.forEach((item) => { //2021-12-06
        // 前端数量
        qianarr.push(myarr.filter((ele) => {
          // console.log(ele)
          return ele.CategoryOne == "1" && item == ele.createAt.substr(0, 10)
        }).length)
        // 后端的数量
        houarr.push(myarr.filter((ele) => {
          return ele.CategoryOne == "2" && item == ele.createAt.substr(0, 10)
        }).length)
        // 视觉的数量
        shiarr.push(myarr.filter(ele => {
          return ele.CategoryOne == "3" && item == ele.createAt.substr(0, 10)
        }).length)
      })
      // console.log(qianarr)
      // console.log(houarr)
      // console.log(shiarr)
      // 所有日期
      //1.初始化echarts 实例 
      var mycharts = this.$echarts.init(document.getElementById("myline"));
      // 2.准备配置项
      var option = {
        title: {
          text: 'Stacked Line'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['后端', '前端', "视觉设计"]
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: darr
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '后端',
            type: 'line',
            data: houarr
          },
          {
            name: '前端',
            type: 'line',
            data: qianarr
          },
          {
            name: '视觉设计',
            type: 'line',
            data: shiarr
          }
        ]
      };
      // 3. 将配置项设置给实例 
      mycharts.setOption(option)
    }
  },
};
</script>

<style lang="less" scoped>
.line_wrap {
  width: 90%;
  margin: 20px auto;
  // background: linear-gradient(blue, green);
  background: white;
  height: 400px;
}
#myline {
  width: 90%;
  margin: 0 auto;
  height: 350px;
  background: white;
}
h1 {
  text-align: center;
}
</style>